// biddings.js
var count_down = require('../../utils/countDown.js');
Page({
  data: {
    advs:[],
    recommend:[],
    inProcess:[],
    newBiddings:[],
    countDown:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAdvs();
    this.getRecommend();    
    wx.setNavigationBarTitle({
      title: '商品竞价'
    })
  },
  //获取广告banner
  getAdvs:function(){
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'webapp/business/advs',
      header: getApp().globalData.header,
      data:{advType:4},
      method: 'POST',
      success: function (res) {
        if(null!=res.data&&typeof(res.data)!='undefined')
          that.setData({
            advs:res.data
          });
      },
      fail: function (res) {},
      complete: function (res) {}
    })
  },
  //获取热门推荐
  getRecommend: function () {
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'bidding/recommendList',
      header: getApp().globalData.header,
      method: 'POST',
      success: function (res) {
        if(null!=res.data&&typeof(res.data)!='undefined'){
          for (var i=0;i<res.data.length;i++){
            count_down.countDown('',res.data[i].expireTime,that,res.data[i].biddingCode);
            that.data.recommend.push(res.data[i]);
          }
          that.setData({
            recommend:that.data.recommend
          });
        }
      },
      fail: function (res) { },
      complete: function (res) { 
        that.getInProcess();
      }
    })
  },
  //获取正在进行的竞价
  getInProcess: function () {
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'bidding/inProcessList',
      header: getApp().globalData.header,
      method: 'POST',
      success: function (res) {
        if(null!=res.data.records&&typeof(res.data.records)!='undefined'){
          for (var i=0;i<res.data.records.length;i++){
            count_down.countDown('',res.data.records[i].expireTime,that,res.data.records[i].biddingCode);
            that.data.inProcess.push(res.data.records[i]);
          }
          that.setData({
            inProcess:that.data.inProcess
          });
        }
      },
      fail: function (res) { },
      complete: function (res) { 
        that.getNewBididing();
      }
    })
  },
  //获取即将开始的竞价
  getNewBididing: function () {
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'bidding/newList',
      header: getApp().globalData.header,
      method: 'POST',
      success: function (res) {
        if(null!=res.data.records&&typeof(res.data.records)!='undefined'){
          for (var i=0;i<res.data.records.length;i++){
            count_down.countDown('',res.data.records[i].expireTime,that,res.data.records[i].biddingCode);
            that.data.newBiddings.push(res.data.records[i]);
          }
          that.setData({
            newBiddings:that.data.newBiddings
          });
        }
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  getBiddingDetail: function (event) {
    var biddingId = event.currentTarget.dataset.biddingId;

    wx.navigateTo({
      url: '../bidding/bidding?biddingId=' + biddingId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})